package com.trkj.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.shamanland.fonticon.FontIconDrawable;
import com.trkj.base.BaseActivity;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.User;
import com.trkj.base.newwork.ProgressBarDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * Created by LiZanhong on 2015/07/08.
 */
public class DeptScoreActivity extends BaseActivity implements ListView.OnItemClickListener{
    public static final String ACTION = "com.trkj.usercenter.DeptScoreActivity";
    private JSONArray data;

    @ViewInject(R.id.list)
    private ListView listView;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Integer userId = (Integer) parent.getAdapter().getItem(position);
        Intent intent = new Intent(ScoreActivity.ACTION);
        intent.putExtra("userId", userId);
        intent.putExtra("userName", data.getJSONObject(position).getString("user_name"));
        startActivity(intent);
    }

    class DeptRandAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.getJSONObject(position).getIntValue("user_id");
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(DeptScoreActivity.this)
                        .inflate(R.layout.two_item_arrow, null);
                viewHolder.key = (TextView)convertView.findViewById(R.id.key);
                viewHolder.value = (TextView)convertView.findViewById(R.id.value);
                viewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.right_arrow);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            if(position == 0){
                viewHolder.key.setText("①" + data.getJSONObject(position).getString("user_name"));
                viewHolder.key.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.green));
                viewHolder.value.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.green));
            }else if(position == 1){
                viewHolder.key.setText("②" + data.getJSONObject(position).getString("user_name"));
                viewHolder.key.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.orange));
                viewHolder.value.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.orange));
            }else if(position == 2){
                viewHolder.key.setText("③" + data.getJSONObject(position).getString("user_name"));
                viewHolder.key.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.yellow));
                viewHolder.value.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.yellow));
            }else{
                viewHolder.key.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.black));
                viewHolder.value.setTextColor(Constants.Color.getColor(DeptScoreActivity.this, R.color.normal_unselected));
                viewHolder.key.setText((position + 1) + ". " + data.getJSONObject(position).getString("user_name"));
            }
            viewHolder.value.setText(data.getJSONObject(position).getString("points") + "分");
            viewHolder.rightArrow.setImageDrawable(FontIconDrawable.inflate(DeptScoreActivity.this, R.xml.right_arrow));

            return convertView;
        }

        class ViewHolder{
            TextView key;
            TextView value;
            ImageView rightArrow;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);
        ViewUtils.inject(this);
        listView.setOnItemClickListener(this);

        ProgressBarDataFromRemote dataFromRemote =
                new ProgressBarDataFromRemote(this, new OnResponseResultListener() {
                    @Override
                    public void onResponseResult(String result) {
                        JSONObject object = JSON.parseObject(result);
                        if(object.getBoolean("success")){
                            data = object.getJSONArray("data");
                            listView.setAdapter(new DeptRandAdapter());
                        }
                    }
                });

        User user = UserService.getInstance(this).parseUser();
        if(user != null){
            int deptId = user.deptId;
            String url = MessageFormat.format(Constants.ApiUrl.DEPT_SCORE_RANK, deptId);
            dataFromRemote.execute(url);
        }
    }

    @Override
    protected String getCustomerTitle() {
        return "部门学分排行榜";
    }
}
